<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #left {
      width: 500px;
      height: 500px;
      border: 1px solid red;
      margin-left: 100px;
      margin-top: 50px;
      float: left;

    }

    #left #small {
      width: 400px;
      height: 400px;
      position: relative;
      text-align: center;
      margin-left: 50px;
    }

    #small img {
      width: 400px;
      height: 400px;
    }

    #mask {
      width: 150px;
      height: 150px;
      background: rgba(255, 255, 255, 0.5);
      cursor: move;
      position: absolute;
      top: 0;
      left: 0;
      display: none;
    }

    .img_list {
      width: 500px;
      height: 100px;
      display: flex;
      justify-content: space-around;
    }

    .img_list img {
      width: 100px;
      height: 100px;
    }

    .img_list .active {
      border: 2px solid red;
      box-sizing: border-box;
    }

    #big {
      width: 500px;
      height: 500px;
      float: left;
      overflow: hidden;
      margin-top: 50px;
      margin-left: 50px;
      display: none;
    }

    #big img {
      width: 1200px;
      height: 1200px;
    }
  </style>
</head>

<body>
  <div id="left">
    <div id="small">
      <img src="./images/1.png" alt="">
      <div id="mask"></div>
    </div>
    <div class="img_list">
      <img class="active" src="./images/1.png" alt="">
      <img src="./images/2.png" alt="">
      <img src="./images/3.png" alt="">
      <img src="./images/4.png" alt="">
    </div>
  </div>
  <div id="big">
    <img src="./images/1.png" alt="">
  </div>


  <script src="./jquery-3.4.1.min.js"></script>
  <script>
    // 小图切换
    $(".img_list img").click(function () {
      $(this).addClass("active").siblings().removeClass("active");
      // 把当前点击的图片地址 赋值给小盒子的图片地址
      $("#small img").attr("src", $(this).attr("src"))
      $("#big img").attr("src", $(this).attr("src"))
    })
    //小盒子的鼠标移动事件
    $("#small").mousemove(function (e) {
      // e.pageX e.pageY 
      var x = e.pageX - $("#small").offset().left - $("#mask").width() / 2;
      var y = e.pageY - $("#small").offset().top - $("#mask").height() / 2;
      // 限制最小
      x < 0 ? x = 0 : x;
      y < 0 ? y = 0 : y;
      // 限制最大
      x > $("#small").width() - $("#mask").width() ? x = $("#small").width() - $("#mask").width() : x;
      y > $("#small").height() - $("#mask").height() ? y = $("#small").height() - $("#mask").height() : y
      $("#mask").css({
        left: x + "px",
        top: y + "px"
      })
      // 算出大图片和小盒子的宽高比
      var bilix = $("#big img").width() / $("#small").width();
      var biliy = $("#big img").height() / $("#small").height();
      //成比例  改变大盒子的scrollTop
      $("#big").scrollLeft(bilix * x)
      $("#big").scrollTop(biliy * y)
    })
    // 鼠标移入移出的显示隐藏
    $("#small").mouseenter(function () {
      $("#big").show();
      $("#mask").show()
    }).mouseleave(function () {
      $("#big").hide();
      $("#mask").hide()
    })






  </script>
</body>

</html>